/**
 * ...
 * @author Thot
 */

function LayersScreen()
{
}


	//static
	LayersScreen.ON_SELECT			= "onSelect";
	LayersScreen.ADD_LAYER			= "addLayer";
	LayersScreen.REMOVE_LAYER		= "removeLayer";

	// private
	LayersScreen.prototype._list			= null;
	LayersScreen.prototype._addButton		= null;
	LayersScreen.prototype._removeButton	= null;

	LayersScreen.prototype._unSelectAll = function()
	{
		var link = null;
		var linkList = this._list.getElementsByTagName("a");
		var i = 0;
		var max = linkList.length;
		
		for( ; i < max; i++ )
		{
			link = linkList[i];
			link.setAttribute( "class", "" );
		}
	};
	
	
	
	// public
	LayersScreen.prototype.signal		= null;
	
	LayersScreen.prototype.init = function( p_list, p_addButton, p_removeButton )
	{
		this.signal 		= new Signal();
		this._list 			= p_list;
		this._addButton		= p_addButton;
		this._removeButton	= p_removeButton;
		this._list.addEventListener( "click", CallBack.create( this._clickHandler, this ) );
		this._addButton.addEventListener( "click", CallBack.create( this._addHandler, this ) );
		this._removeButton.addEventListener( "click", CallBack.create( this._removeHandler, this ) );
		this.clean();
	};
	
	LayersScreen.prototype._clickHandler = function(event)
	{
		var node 	= event.target;
		var layerId = node.innerHTML;
		this.signal.dispatchEvent( LayersScreen.ON_SELECT, [layerId] );
		this._unSelectAll();
		node.setAttribute("class", "selected");
	};
	
	LayersScreen.prototype._addHandler = function(event)
	{
		var first = ( this._list.children.length == 0 );
		
		var layerId = "layer_"+( this._list.children.length );
		this.signal.dispatchEvent( LayersScreen.ADD_LAYER, [layerId] );
		this._unSelectAll();
		
		if( first )
		{
			var node = this._list.children[0].children[0];
			this.signal.dispatchEvent( LayersScreen.ON_SELECT, [layerId] );
			node.setAttribute("class", "selected");
		}
	};
	
	LayersScreen.prototype._removeHandler = function(event)
	{
		this.signal.dispatchEvent( LayersScreen.REMOVE_LAYER );
		this._unSelectAll();
	};
	

	LayersScreen.prototype.addLayer = function( p_layerId )
	{
		var li 		= document.createElement("li");
		var link 	= document.createElement("a");
		
		var className = ( this._list.children.length % 2 == 0 ) ? "impair" : "pair";
		link.setAttribute( "class", className );
		link.setAttribute( "href", "#" );
		link.innerHTML = p_layerId;
		
		li.appendChild( link );
		this._list.appendChild( li );
	};
	
	LayersScreen.prototype.removeLayer = function( p_layerId )
	{
		var li 		= null;
		var link 	= null;
		var i = 0;
		var max = this._list.children.length;
		var toKill = new Array();
		
		for( i = 0; i < max; i++ )
		{
			li = this._list.children[i];
			link = li.children[0];
			
			if( link.innerHTML == p_layerId )
			{
				toKill.push(li);
			}
		}
		
		max = toKill.length;
		i = 0;
		
		for( i = 0; i < max; i++ )
		{
			this._list.removeChild(toKill[i]);
		}
	};
	
	LayersScreen.prototype.clean = function()
	{
		while( this._list.children.length > 0 )
		{
			this._list.removeChild( this._list.children[0] );
		}
	};
	
	
